package models;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Statement;

import javax.xml.bind.DatatypeConverter;
import com.mysql.jdbc.ResultSet;

import utils.DAO;
import utils.DAOUser;

public class BeanUser {

	private String user = "";
	private String mail = "";
	private String nombre = "";
	private String apellidos = "";
	private String sexo = "";
	private String password = "";
	private String day = "";
	private String month = "";
	private String year = "";
	private int[] error = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	/* Getters */
	public String getUser() {
		return user;
	}

	public String getMail() {
		return mail;
	}

	public int[] getError() {
		return error;
	}

	public String getNombre() {
		return nombre;
	}

	public String getApellidos() {
		return apellidos;
	}

	public String getSexo() {
		return sexo;
	}

	public String getPassword() {
		return password;
	}

	public String getDay() {
		return day;
	}

	public String getMonth() {
		return month;
	}

	public String getYear() {
		return year;
	}

	/* Setters */
	public void setUser(String user) throws Exception {

		DAOUser daoUser = new DAOUser();
		boolean exist = daoUser.userExists(user);

		if (exist)
			error[0] = 1;
		else
			this.user = user;
	}

	public void setMail(String mail) throws Exception {
		boolean exist = false;
		DAO dao = new DAO();
		Statement sentencia = dao.getConnexio().createStatement();
		ResultSet rs = (ResultSet) sentencia.executeQuery("SELECT mail FROM user;");

		while (rs.next()) {
			if (mail.equals(rs.getString("mail")))
				exist = true;
		}

		if (exist)
			error[1] = 1;
		else
			this.mail = mail;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public void setApellidos(String apellidos) {
		this.apellidos = apellidos;
	}

	public void setSexo(String sexo) {
		this.sexo = sexo;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setDay(String day) {
		this.day = day;
		if (!isDateValid()) {
			error[7] = 1;
		}
	}

	public void setMonth(String month) {
		this.month = month;
		if (!isDateValid()) {
			error[8] = 1;
		}
	}

	public void setYear(String year) {
		this.year = year;
	}

	/* Logic Functions */

	public boolean isComplete() {
		return (hasValue(getMail()) && hasValue(getNombre())
				&& hasValue(getSexo()) && hasValue(getDay())
				&& hasValue(getYear()) && hasValue(getMonth())
				&& hasValue(getApellidos()) && hasValue(getUser())
				&& hasValue(getPassword()) && isDateValid());
	}

	private boolean hasValue(String val) {
		return ((val != null) && (!val.equals("")));
	}

	private boolean isDateValid() {
		boolean valid = true;
		if (!day.equals("") && !month.equals("")) {
			if (month.equals("2")) {
				if (Integer.parseInt(day) > 28) {
					valid = false;
				}
			}
			if (month.equals("4") || month.equals("6") || month.equals("9")
					|| month.equals("11")) {
				if (Integer.parseInt(day) > 30) {
					valid = false;
				}
			}
		}
		return valid;
	}

	public String generateActivationCode() throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		md.update(getUser().getBytes());
		byte[] hash = md.digest(getPassword().getBytes());

		String code = DatatypeConverter.printBase64Binary(hash);

		return code;

	}

}
